﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
using Quartz;
using Quartz.Impl;

namespace CustomsNuke.Scheduling
{
    public class Global
    {
        private static object syncLock = new object();
        private static Global _instance = null;
        public static Global Current
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Global();
                        }
                    }
                }
                return _instance;
            }
        }



        private IScheduler _quartzScheduler = null;

        public IScheduler Scheduler
        {
            get
            {
                if (_quartzScheduler == null)
                {
                    InitialScheduler();
                }
                return _quartzScheduler;
            }
        }

        private void InitialScheduler()
        {
            NameValueCollection properties = new NameValueCollection();

            properties["quartz.scheduler.instanceName"] = Constant.InstanceName;
            properties["quartz.scheduler.instanceId"] = Constant.InstanceId;
            properties["quartz.threadPool.type"] = Constant.ThreadPoolType;
            properties["quartz.threadPool.threadCount"] = Constant.ThreadCount;
            properties["quartz.threadPool.threadPriority"] = Constant.ThreadPriority;
            properties["quartz.jobStore.driverDelegateType"] = Constant.DriverDelegateType;
            properties["quartz.jobStore.misfireThreshold"] = Constant.MisfireThreshold;
            properties["quartz.jobStore.type"] = Constant.JobType;
            properties["quartz.jobStore.useProperties"] = Constant.UseProperties;
            properties["quartz.jobStore.dataSource"] = Constant.DataSource;
            properties["quartz.jobStore.tablePrefix"] = Constant.TablePrefix;
            properties["quartz.jobStore.clustered"] = Constant.Clustered;
            properties["quartz.jobStore.clusterCheckinInterval"] = Constant.ClusterCheckinInterval;
            properties["quartz.dataSource.default.connectionString"] = Constant.ConnectionString;
            properties["quartz.dataSource.default.provider"] = Constant.Provider;

            ISchedulerFactory schedulerFactory = new StdSchedulerFactory(properties);

            _quartzScheduler = schedulerFactory.GetScheduler();
        }


    }

}
